import { Injectable } from '@nestjs/common';
import { CreateClassroomDto } from './dto/create-classroom.dto';
import { UpdateClassroomDto } from './dto/update-classroom.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Classroom } from './entities/classroom.entity';
import { Repository } from 'typeorm';
import { generateUniqueId } from 'src/utils/upique_id';

@Injectable()
export class ClassroomService {
  constructor(
    @InjectRepository(Classroom)
    private readonly classroomRepository: Repository<Classroom>,
  ) {}

  // 创建班级
  async create(classroom: CreateClassroomDto): Promise<any> {
     await this.classroomRepository.save({
      ...classroom,
      class_id:  `class_${generateUniqueId(10)}`,
    });
    
    return {
      status: 200,
      message: '班级创建成功',
    };
  }
  // 查询目标班级所有学生
  async findClassWithStudents(classId: string): Promise<any> {
    let result = await this.classroomRepository.findOne({
      where: { class_id: classId },
      relations: ['students'], // 加载关联的学生
    });
    return {
      data: result,
      status: 200,
      message: '查询目标班级所有学生',
    };
  }
}
